cmake_minimum_required(VERSION 3.14)

# Version String Info
# Alpha: x.x.x.1xx
# Beta: x.x.x.2xx
# Release Preview: x.x.x.3xx
# Release: x.x.x.4xx
project(SynoMusicPlayer VERSION 0.0.1.10 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    set(CMAKE_CXX_FLAGS "-fcoroutines ${CMAKE_CXX_FLAGS}")
endif()

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Quick Qml Sql Network LinguistTools)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Quick Qml Sql Network LinguistTools)

# 获取 Commit Hash
execute_process(COMMAND git rev-parse HEAD
                WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
                TIMEOUT 10
                OUTPUT_VARIABLE GIT_HASH
                OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS "Git Commit Hash: ${GIT_HASH}")

# 生成版本号文件
configure_file(
  "Sources/Inc/Version.h.in"
  "${CMAKE_CURRENT_SOURCE_DIR}/Sources/Inc/Version.h"
  )

# 导入外部库
add_subdirectory(ThirdParty/fmt EXCLUDE_FROM_ALL)

set(FLUENTUI_BUILD_EXAMPLES OFF)
add_subdirectory(ThirdParty/FluentUI EXCLUDE_FROM_ALL)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/libhv/include/hv)

#导入exmaple的QML位置，不然import example有时候会爆红
set(QML_IMPORT_PATH ${CMAKE_BINARY_DIR}/SynoMusicPlayer CACHE STRING "Qt Creator extra QML import paths" FORCE)

#遍历所有Cpp文件
file(GLOB_RECURSE sources_files "Sources/Src/*.cpp" "Sources/Inc/*.h" "Sources/Src/**/*.cpp" "Sources/Inc/**/*.h")

#遍历所有qml文件
file(GLOB_RECURSE QML_PATHS "Views/*.qml" "Views/**/*.qml" )
foreach(filepath ${QML_PATHS})
    string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" filename ${filepath})
    list(APPEND qml_files ${filename})
endforeach(filepath)

#遍历所有资源文件
file(GLOB_RECURSE RES_PATHS "Resources/*.png" "Resources/*.jpg" "Resources/*.svg" "Resources/*.ico" "Resources/*.ttf" "Resources/*.webp" "Views/qmldir" "Views/**/qmldir")
foreach(filepath ${RES_PATHS})
    string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" filename ${filepath})
    list(APPEND resource_files ${filename})
endforeach(filepath)

if(${CMAKE_HOST_SYSTEM_NAME} EQUAL "Windows")
  #添加可执行文件
  qt_add_executable(SynoMusicPlayer
      ${sources_files}
      SynoMusic.rc
  )
else()
  #添加可执行文件
  qt_add_executable(SynoMusicPlayer
      ${sources_files}
  )
endif()

#添加qml模块
qt_add_qml_module(SynoMusicPlayer
  URI "SynoMusicPlayer"
    VERSION 1.0
    QML_FILES ${qml_files}
    RESOURCES ${resource_files}
    RESOURCE_PREFIX "/"
)


target_link_libraries(SynoMusicPlayer PRIVATE
    fmt::fmt
    Qt${QT_VERSION_MAJOR}::Core
    Qt${QT_VERSION_MAJOR}::Quick
    Qt${QT_VERSION_MAJOR}::Qml
    Qt${QT_VERSION_MAJOR}::Sql
    Qt${QT_VERSION_MAJOR}::Network
    fluentuiplugin
)

target_include_directories(SynoMusicPlayer PRIVATE "Sources/Inc")

#导入component头文件,不然通过QML_NAMED_ELEMENT生成的c++类会找不到头文件报错
# target_include_directories(SynoMusicPlayer PRIVATE
#     ${CMAKE_CURRENT_SOURCE_DIR}/src/component
# )

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
set_target_properties(SynoMusicPlayer PROPERTIES
    MACOSX_BUNDLE_GUI_IDENTIFIER com.example.SynoMusicPlayer
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)


#Release也支持日志打印代码位置
target_compile_definitions(SynoMusicPlayer
    PRIVATE
    QT_MESSAGELOGCONTEXT
)

include(GNUInstallDirs)
install(TARGETS SynoMusicPlayer
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
